package com.kafka;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class KafkaProducerService {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendMessage(String topic, String message) {
        kafkaTemplate.send(topic, message).addCallback(
                result -> System.out.println("消息发送成功：" + result.getRecordMetadata().offset()),
                ex -> System.err.println("消息发送失败：" + ex.getMessage())
        );
    }
}
